import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { PersonSelectComponent } from '../person-select/person-select.component';

@Component({
  selector: 'app-watch-bind-code-list',
  templateUrl: './watch-bind-code-list.component.html',
  styles: []
})
export class WatchBindCodeListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
    this.config.defaultParam={"id":value.id}
  };
  param: any;
  deviceType:string;
  submitDeviceType:string;
  show:boolean=false;
  config: SPage = {
    columns: [
      // {
      //   index: 'name',
      //   title: '用户名称',
      //   filter: {
      //     placeholder: '请输入用户名',
      //     type: 'string'
      //   }
      // },
      // {
      //   index: 'phone',
      //   title: '联系电话',
      //   filter: {
      //     placeholder: '请输入联系电话',
      //     type: 'string'
      //   }
      // },
      {
        index: 'qcCode',
        title: '二维码sn',
        filter: {
          placeholder: '请输入二维码sn',
          type: 'string'
        }
      },
      {
        index: 'isAdmin',
        title: '管理员',
        type:ColumnType.tag,
        tag:{
          true:{text: '是'},
          false:{text: '否'}
        },
        filter:{
            placeholder:'是否管理员',
            type:'select',
            items:[
              {label:"是",value:1},
              {label:"否",value:0},
            ]
        }
      },
      {
        index: 'description',
        title: '描述',
      },
      {
        index: 'approveStatus',
        title: '审批状态',
        type:ColumnType.tag,
        tag:{
          0:{text:"未审批",color:"blue"},
          1:{text:"审批通过",color:"green"},
          2:{text:"驳回",color:"red"},
          3:{text:"待认证",color:"blue"}
        },
        filter: {
          placeholder: '请选择状态',
          type: 'select',
          items:[
            {label:"未审批",value:0},
            {label:"审批通过",value:1},
            {label:"驳回",value:2},
            {label:"待认证",value:3}
          ]
        }
      },
      {
        index: 'createTime',
        title: "创建时间",
        type:ColumnType.date
      },
      {
        index: '',
        title: '操作',
        fixed:'right',
        buttons:[
          {
            text:'解绑二维码',
            iif:(data:STData)=>{
              return data.approveStatus == 1
            },
            click:(_record)=>{
              this.unbindCode(_record)
            }
          },
          {
            text:'转换管理员',
            iif:(data:STData)=>{
              return data.isAdmin && data.approveStatus == 1
            },
            click:(_record)=>{
              this.editDic(_record,1)
            }
          },
          {
            text:'升级管理员',
            iif:(data:STData)=>{
              return !data.isAdmin && data.approveStatus == 1
            },
            click:(_record)=>{
              this.updateAdmin(_record)
            }
          },
          {
            text:'审批',
            iif:(data:STData)=>{
              return data.approveStatus == 0
            },
            click:(_record)=>{
              this.approvePerson(_record,1)
            }
          },
          {
            text:'驳回',
            iif:(data:STData)=>{
              return data.approveStatus == 0
            },
            click:(_record)=>{
              this.approvePerson(_record,2)
            }
          },
        ]
      },
    ],
    showCheckBox: false,
    reqUrl:'/core/qrcode/qcCodeListByPeople',
  }
  constructor(private http: _HttpClient,  private api: ApiService,private ref: NzModalRef, private modal: NzModalService, private message: NzMessageService) { }
  ngOnInit() {
    
  }
  _console(e: any): void {
    this.selectData = e;
  }
  dataProcess(data: STData[]){
    return data;
  }
  // // 编辑功能开始
  editDic( _param?: object,type?:number): void {
    const modal = this.modal.create({
      nzTitle: "转换管理员",
      nzContent: PersonSelectComponent,
      nzWidth: 1300,
      nzComponentParams: {
        type,
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  //解绑二维码
  async unbindCode(param): Promise<void> {
    //先判断当前用户是否是管理员，
    if(param.isAdmin){
      //如果是管理员，再去判断这个二维码下有没有其他人员，
     const personFlag = await this.queryCodePerson(param)
     if(personFlag){
      //如果有则选择人员之后进行解绑
      this.editDic(param,2)
     }else{
      //如果没有，则直接进行解绑
      this.unbindCallBack({id:param.id})
     }
    }else{
      //如果不是管理员，则直接解绑
      this.unbindCallBack({id:param.id})
    }
  
  }
  //查询二维码下是否绑定了其他人员
  queryCodePerson(data){
    return new Promise((resolve,reject)=>{
        this.http.request("post","/core/qrcode/peopleListByQcCode",{body:{"qcCode":data.qcCode,"isAdmin":0}}).subscribe((res:any) => {
          if (res.status === '200' && res.data.list.length > 0) {
              resolve(true)
          }else if(res.data.list.length == 0){
              resolve(false)
          }else{
            resolve(false)
          }
        })
      })
  }
  //查询二维码的管理员
  queryCodeAdmin(data){
    return new Promise((resolve,reject)=>{
      this.http.request("post","/core/qrcode/peopleListByQcCode",{body:{"qcCode":data.qcCode,"isAdmin":1}}).subscribe((res:any) => {
        if (res.status === '200' && res.data.list.length > 0) {
            resolve(res.data.list[0])
        }else if(res.data.list.length == 0){
            resolve(null)
        }else{
          resolve(null)
        }
      })
    })
  }
  unbindCallBack(data){
    this.modal.confirm({
      nzTitle: '确定解绑?',
      nzOnOk: () => {
        this.http.request("post","/core/qrcode/unbindQrCode",{body:data}).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
          }else{
            this.message.error(res.message)
          }
        })
      }
    })
  }
  //升级管理员
  async updateAdmin(param): Promise<void> {
    const data:any=await this.queryCodeAdmin(param)
    this.modal.confirm({
      nzTitle: '此二维码现在的管理员是'+data.name+',您确定将'+param.name+'升级为管理员?',
      nzOnOk: () => {
        this.http.request("post","/core/qrcode/upgradeAdmin",{body:{id:param.id}}).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
            // this.st.st.reset({...this.st.st.req.params});
          }else{
            this.message.error(res.message)
          }
        })
      }
    })
  }
  //审批或驳回
  approvePerson(param,num):void{
    this.modal.confirm({
      nzTitle: '是否确定?',
      nzOnOk: () => {
        this.http.request("post","/core/qrcode/approve",{body:{id:param.id,approveStatus:num}}).subscribe((res:any) => {
          if (res.status === '200') {
            this.message.success(res.message);
            this.st.reload();
            // this.st.st.reset({...this.st.st.req.params});
          }else{
            this.message.error(res.message)
          }
        })
      }
    })
  }

}
